//封装导航栏组件
//实现的目标
//搜索，购物车，用户的菜单，折叠菜单



<template>
  <nav class="navbar">
    <div class="container">
      <div class="logo">
        <router-link to="/">ShopVue</router-link>
      </div>
      <div class="nav-links">
        <router-link to="/" :class="{ active: $route.path === '/' }">首页</router-link>
        <router-link to="/products" :class="{ active: $route.path === '/products' }">商品</router-link>
        <router-link to="/about" :class="{ active: $route.path === '/about' }">关于我们</router-link>
        <router-link to="/contact" :class="{ active: $route.path === '/contact' }">联系我们</router-link>
      </div>

      <!-- 功能区域 -->
      <div class="actions">
        <!-- 搜索框 -->
        <div class="search">
          <input 
            type="text" 
            placeholder="搜索商品..." 
            v-model="searchQuery"
            @keyup.enter="handleSearch"
          >
          <button @click="handleSearch">
            <i class="fas fa-search"></i>
          </button>
        </div>

        <!-- 购物车 -->
        <router-link to="/cart" class="cart" v-if="userStore.isAuthenticated">
          <i class="fas fa-shopping-cart"></i>
          <span v-if="cartStore.itemCount > 0">{{ cartStore.itemCount }}</span>
        </router-link>

        <!-- 用户菜单 -->
        <div class="user-menu">
          <template v-if="userStore.isAuthenticated">
            <div class="user-info">
              <span>{{ userStore.userInfo.username }}</span>
              <i class="fas fa-caret-down" @click="showUserMenu = !showUserMenu"></i>
              
              <div class="dropdown" v-if="showUserMenu">
                <router-link to="/profile">个人中心</router-link>
                <router-link to="/orders">我的订单</router-link>
                <button @click="handleLogout">退出登录</button>
              </div>
            </div>
          </template>
          
          <template v-else>
            <router-link to="/login">登录</router-link>
            <router-link to="/register">注册</router-link>
          </template>
        </div>

        //移动菜单
        <button class="mobile-menu-btn" @click="showMobileMenu = !showMobileMenu">
          <i class="fas fa-bars"></i>
        </button>
      </div>
    </div>
    <div class="mobile-menu" v-if="showMobileMenu">
      <router-link to="/" @click="showMobileMenu = false">首页</router-link>
      <router-link to="/products" @click="showMobileMenu = false">商品</router-link>
      <router-link to="/about" @click="showMobileMenu = false">关于我们</router-link>
      <router-link to="/contact" @click="showMobileMenu = false">联系我们</router-link>
      <router-link to="/cart" @click="showMobileMenu = false" v-if="userStore.isAuthenticated">购物车</router-link>
      <router-link to="/login" @click="showMobileMenu = false" v-if="!userStore.isAuthenticated">登录</router-link>
      <router-link to="/register" @click="showMobileMenu = false" v-if="!userStore.isAuthenticated">注册</router-link>
    </div>
  </nav>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import { useCartStore } from '../stores/cart'
const userStore = useUserStore()
const cartStore = useCartStore();const searchQuery = ref('')
const showUserMenu = ref(false)
const router = useRouter()

const showMobileMenu = ref(false)

// 搜索
const handleSearch = () => {
  if (searchQuery.value.trim()) {
    router.push({
      name: 'products',
      query: { search: searchQuery.value.trim() }
    })
  }
}

// 退出登录
const handleLogout = () => {
  userStore.logout()
  router.push('/login')
  showUserMenu.value = false
}
</script>

<style scoped>
.navbar {
  background-color: #fff;
  border-bottom: 1px solid #eee;
  padding: 15px 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.logo a {
  font-size: 1.5rem;
  font-weight: bold;
  color: #42b983;
  text-decoration: none;
}

 导航链接 
.nav-links {
  display: flex;
  gap: 25px;
  margin: 0 20px;
}

.nav-links a {
  color: #333;
  text-decoration: none;
  padding: 5px 0;
  position: relative;
}

.nav-links a.active {
  color: #42b983;
}

.nav-links a.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #42b983;
}
.actions {
  display: flex;
  align-items: center;
  gap: 15px;
}
//搜索区域
.search {
  display: flex;
  align-items: center;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 5px 10px;
}

.search input {
  border: none;
  outline: none;
  padding: 5px;
  min-width: 180px;
}

.search button {
  background: none;
  border: none;
  color: #666;
  cursor: pointer;
}

/*购物*/
.cart {
  color: #333;
  text-decoration: none;
  position: relative;
  font-size: 1.2rem;
}

.cart span {
  position: absolute;
  top: -8px;
  right: -8px;
  background-color: #e74c3c;
  color: white;
  font-size: 0.7rem;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 用户 */
.user-menu {
  display: flex;
  align-items: center;
  gap: 15px;
}

.user-menu a {
  color: #333;
  text-decoration: none;
}

.user-info {
  position: relative;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 5px;
}

.dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  background-color: white;
  border: 1px solid #eee;
  border-radius: 4px;
  width: 150px;
  padding: 10px 0;
  z-index: 100;
}

.dropdown a, .dropdown button {
  display: block;
  width: 100%;
  padding: 8px 15px;
  text-align: left;
  background: none;
  border: none;
  cursor: pointer;
  text-decoration: none;
  color: #333;
}

.dropdown a:hover, .dropdown button:hover {
  background-color: #f5f5f5;
  color: #42b983;
}
/* 按钮 */
.mobile-menu-btn {
  background: none;
  border: none;
  font-size: 1.5rem;
  color: #333;
  display: none;
  cursor: pointer;
}
.mobile-menu {
  display: none;
  padding: 15px 20px;
  background-color: white;
  border-top: 1px solid #eee;
}

.mobile-menu a {
  display: block;
  color: #333;
  text-decoration: none;
  padding: 10px 0;
  border-bottom: 1px solid #f5f5f5;
}
@media (max-width: 768px) {
  .nav-links, .search {
    display: none;
  }
  
  .mobile-menu-btn, .mobile-menu {
    display: block;
  }
}
</style>
